package com.easyadmin.pro.common.vo;

import com.easyadmin.pro.common.enums.HttpCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * packageName com.easyadmin.pro.common.vo
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className RestResult
 * @date 2024/4/1
 * @description 统一返回对象
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class RestResult<T> {

    private Integer code;

    private String msg;

    private T data;

    /**
     * 构造异常返回对象
     * @param msg
     * @return
     */
    public static RestResult lose(String msg) {
        RestResult restResult = new RestResult();
        return restResult.error(msg);
    }


    public static RestResult lose(HttpCodeEnum httpCodeEnum) {
        RestResult restResult = new RestResult();
        return restResult.error(httpCodeEnum.getCode(), httpCodeEnum.getMsg());
    }

    /**
     * 构造异常返回对象
     * @param code
     * @param msg
     * @return
     */
    public static RestResult lose(Integer code, String msg) {
        RestResult restResult = new RestResult();
        return restResult.error(code, msg);
    }

    public static RestResult success() {
        RestResult restResult = new RestResult();
        return restResult.ok(null);
    }

    /**
     * 构造成功返回对象
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static RestResult success(Integer code, String msg, Object data) {
        RestResult restResult = new RestResult();
        return restResult.ok(code, msg, data);
    }

    /**
     * 构造成功返回对象
     * @param code
     * @param msg
     * @return
     */
    public static RestResult success(Integer code, String msg) {
        RestResult restResult = new RestResult();
        return restResult.ok(code, msg);
    }

    /**
     * 构造成功返回对象
     * @param msg
     * @param data
     * @return
     */
    public static RestResult success(String msg, Object data) {
        RestResult restResult = new RestResult();
        return restResult.ok(msg, data);
    }

    /**
     * 构造成功返回对象
     * @param data
     * @return
     */
    public static RestResult success(Object data) {
        RestResult restResult = new RestResult();
        return restResult.ok(data);
    }


    /**
     * 构造异常返回信息
     * @param msg
     * @return
     */
    private RestResult error(String msg) {
        this.code = HttpCodeEnum.SYSTEM_ERROR.getCode();
        this.msg = msg;
        return this;
    }

    /**
     * 构造异常返回消息
     * @param code
     * @param msg
     * @return
     */
    private RestResult error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    /**
     * 构造成功返回消息
     *
     * @param data
     * @return
     */
    private RestResult<?> ok(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        return this;
    }

    /**
     * 构造成功返回消息
     * @param code
     * @param msg
     * @return
     */
    private RestResult<?> ok(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
        return this;
    }

    /**
     * 构造成功返回消息
     * @param msg
     * @param data
     * @return
     */
    private RestResult<?> ok(String msg, T data) {
        this.code = HttpCodeEnum.SUCCESS.getCode();
        this.msg = msg;
        this.data = data;
        return this;
    }

    /**
     * 构造成功返回消息
     * @param data
     * @return
     */
    private RestResult<?> ok(T data) {
        this.code = HttpCodeEnum.SUCCESS.getCode();
        this.msg = HttpCodeEnum.SUCCESS.getMsg();
        this.data = data;
        return this;
    }
}